import { mergeLocalCart, findCartList, insertCart, deleteCart, updateCart, checkAllCart } from '@/api/cart'
// 购物车状态
export default {
	namespaced: true,
	state: () => ({
		list: []
	}),

	getters: {
		// 有效商品列表
		validList: state => state.list.filter(item => item.isEffective),
		// 选中商品列表
		validSelect: (state, getters) => getters.validList.filter(item => item.selected),
		// 选中商品总价
		validSelectTotal: (state, getters) => getters.validSelect.reduce((sum, item) => (sum += item.count * item.nowPrice), 0),
		//  有效商品是否全部选中状态
		isAll: (state, getters) => getters.validList.every(item => item.selected)
	},

	mutations: {
		addCart (state, good) {
			// 加入过，累加数量；没加过，新增
			const addIndex = state.list.findIndex(item => item.skuId === good.skuId)
			if (addIndex > -1) {
				// 加入过
				state.list[addIndex].count += good.count
			} else {
				state.list.unshift(good)
			}
		},

		singelCheck (state, { good, isChecked }) {
			// 找到要修改的数据
			state.list.find(item => item.skuId === good.skuId).selected = isChecked
		},

		allCheck (state, selected) {
			state.list.forEach(item => (item.selected = selected))
		},

		delCart (state, good) {
			const delIndex = state.list.findIndex(item => item.skuId === good.skuId)
			state.list.splice(delIndex, 1)
		},

		updateNum (state, { good, num }) {
			state.list.find(item => item.skuId === good.skuId).count = num
		},

		// 存储从服务器获取的数据
		setList (state, list) {
			state.list = list
		}
	},

	actions: {

		async getListAction ({ commit }) {
			const res = await findCartList()
			commit('setList', res)
		},

		async mergeCartAction ({ commit, state }) {
			if (state.list.length > 0) {
				const mergeData = state.list.map(item => {
					return {
						skuId: item.skuId,
						selected: item.selected,
						count: item.count
					}
				})
				await mergeLocalCart(mergeData)
			}

			// 从服务器获取购物车列表数据...
			const res = await findCartList()
			commit('setList', res)
		},

		async addCartAction ({ commit, rootState }, good) {
			// 判断是否登录 rootState => store.state
			if (rootState.user.profile.token) {
				await insertCart({ skuId: good.skuId, count: good.count })
				// 从服务器获取购物车列表数据...
				const res = await findCartList()
				commit('setList', res)
				return '加入购物车成功'
			} else {
				commit('addCart', good)
				return '加入购物车成功'
			}
		},

		async singelCheckAction ({ commit, rootState }, { good, isChecked }) {
			// 判断是否登录 rootState => store.state
			if (rootState.user.profile.token) {
				await updateCart({ ...good, selected: isChecked })

				// 从服务器获取购物车列表数据...
				const res = await findCartList()
				commit('setList', res)
			} else {
				commit('singelCheck', { good, isChecked })
			}
		},

		async allCheckAction ({ commit, rootState, getters }, selected) {
			// 判断是否登录 rootState => store.state
			if (rootState.user.profile.token) {
				const ids = getters.validList.map(item => item.skuId)
				await checkAllCart({ selected, ids })

				// 从服务器获取购物车列表数据...
				const res = await findCartList()
				commit('setList', res)
			} else {
				commit('allCheck', selected)
			}
		},

		async delCartAction ({ commit, rootState }, good) {
			// 判断是否登录 rootState => store.state
			if (rootState.user.profile.token) {
				await deleteCart([good.skuId])

				// 从服务器获取购物车列表数据...
				const res = await findCartList()
				commit('setList', res)
			} else {
				commit('delCart', good)
			}
		},

		async updateNumAction ({ commit, rootState }, { good, num }) {
			// 判断是否登录 rootState => store.state
			if (rootState.user.profile.token) {
				await updateCart({ ...good, count: num })

				// 从服务器获取购物车列表数据...
				const res = await findCartList()
				commit('setList', res)
			} else {
				commit('updateNum', { good, num })
			}
		}
	}
}
